<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="发票代码" prop="invoiceCode">
        <el-input
          v-model="queryParams.invoiceCode"
          placeholder="请输入发票代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票号码" prop="invoiceNumber">
        <el-input
          v-model="queryParams.invoiceNumber"
          placeholder="请输入发票号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数电发票号码" prop="eInvoiceNumber">
        <el-input
          v-model="queryParams.eInvoiceNumber"
          placeholder="请输入数电发票号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="销方识别号" prop="sellerTaxId">
        <el-input
          v-model="queryParams.sellerTaxId"
          placeholder="请输入销方识别号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销方名称" prop="sellerName">
        <el-input
          v-model="queryParams.sellerName"
          placeholder="请输入销方名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="购方识别号" prop="buyerTaxId">
        <el-input
            v-model="queryParams.buyerTaxId"
            placeholder="请输入购方识别号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="购买方名称" prop="buyerName">
        <el-input
            v-model="queryParams.buyerName"
            placeholder="请输入购买方名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开票日期" prop="invoiceDate">
        <el-date-picker clearable
                        v-model="queryParams.invoiceDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择开票日期">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="发票来源" prop="invoiceSource">
        <el-input
          v-model="queryParams.invoiceSource"
          placeholder="请输入发票来源"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="发票票种" prop="invoiceType">
          <el-select v-model="queryParams.invoiceType" placeholder="请选择发票状态" clearable>

            <el-option
              v-for="dict in dict.type.invoice_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="发票状态" prop="invoiceStatus">
        <el-select v-model="queryParams.invoiceStatus" placeholder="请选择发票状态" clearable>

          <el-option
            v-for="dict in dict.type.invoice_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否正数发票" prop="isPositiveInvoice">
        <el-input
            v-model="queryParams.isPositiveInvoice"
            placeholder="请输入是否正数发票"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="发票风险等级" prop="invoiceRiskLevel">
        <el-input
          v-model="queryParams.invoiceRiskLevel"
          placeholder="请输入发票风险等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="开票人" prop="invoicePerson">
        <el-input
            v-model="queryParams.invoicePerson"
            placeholder="请输入开票人"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="税收分类编码" prop="taxCategoryCode">
        <el-input
          v-model="queryParams.taxCategoryCode"
          placeholder="请输入税收分类编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="特定业务类型" prop="specificBusinessType">
        <el-select v-model="queryParams.specificBusinessType" placeholder="请选择特定业务类型" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="货物或应税劳务名称" prop="goodsName">
        <el-input
            v-model="queryParams.goodsName"
            placeholder="请输入货物或应税劳务名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specificationModel">
        <el-input
            v-model="queryParams.specificationModel"
            placeholder="请输入规格型号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
            v-model="queryParams.unit"
            placeholder="请输入单位"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="quantity">
        <el-input
            v-model="queryParams.quantity"
            placeholder="请输入数量"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单价" prop="price">
        <el-input
            v-model="queryParams.price"
            placeholder="请输入单价"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="金额" prop="amount">
        <el-input
            v-model="queryParams.amount"
            placeholder="请输入金额"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="税率" prop="taxRate">
        <el-input
            v-model="queryParams.taxRate"
            placeholder="请输入税率"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="税额" prop="taxAmount">
        <el-input
            v-model="queryParams.taxAmount"
            placeholder="请输入税额"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="价税合计" prop="totalAmount">
        <el-input
            v-model="queryParams.totalAmount"
            placeholder="请输入价税合计"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="部门状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择部门状态" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['md:invoice:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['md:invoice:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['md:invoice:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['md:invoice:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="invoiceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="serialNum" />
      <!-- <el-table-column label="销项发票唯一值id" align="center" prop="outId" /> -->
      <el-table-column label="发票代码" align="center" prop="invoiceCode" />
      <el-table-column label="发票号码" align="center" prop="invoiceNumber" />
      <el-table-column label="数电发票号码" align="center" prop="eInvoiceNumber" />
      <el-table-column label="销方识别号" align="center" prop="sellerTaxId" />
      <el-table-column label="销方名称" align="center" prop="sellerName" />
      <el-table-column label="购方识别号" align="center" prop="buyerTaxId" />
      <el-table-column label="购买方名称" align="center" prop="buyerName" />
      <el-table-column label="开票日期" align="center" prop="invoiceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.invoiceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发票来源" align="center" prop="invoiceSource" />
      <el-table-column label="发票票种" align="center" prop="invoiceType" />
      <el-table-column label="发票状态" align="center" prop="invoiceStatus" />
      <el-table-column label="是否正数发票 (1:是, 0:否)" align="center" prop="isPositiveInvoice" />
      <el-table-column label="发票风险等级" align="center" prop="invoiceRiskLevel" />
      <el-table-column label="开票人" align="center" prop="invoicePerson" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="税收分类编码" align="center" prop="taxCategoryCode" />
      <el-table-column label="特定业务类型" align="center" prop="specificBusinessType" />
      <el-table-column label="货物或应税劳务名称" align="center" prop="goodsName" />
      <el-table-column label="规格型号" align="center" prop="specificationModel" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="税率" align="center" prop="taxRate" />
      <el-table-column label="税额" align="center" prop="taxAmount" />
      <el-table-column label="价税合计" align="center" prop="totalAmount" />
      <el-table-column label="部门状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['md:invoice:edit']"
          >修改</el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['md:invoice:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改销项发票对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="850px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20"> <!-- 使用栅格系统布局 -->

          <!-- 基础信息 -->
          <el-col :span="12">
            <el-form-item label="发票代码" prop="invoiceCode">
              <el-input v-model="form.invoiceCode" placeholder="请输入发票代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票号码" prop="invoiceNumber">
              <el-input v-model="form.invoiceNumber" placeholder="请输入发票号码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数电发票号码" prop="eInvoiceNumber">
              <el-input v-model="form.eInvoiceNumber" placeholder="请输入数电发票号码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销方识别号" prop="sellerTaxId">
              <el-input v-model="form.sellerTaxId" placeholder="请输入销方识别号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="销方名称" prop="sellerName">
              <el-input v-model="form.sellerName" placeholder="请输入销方名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="购方识别号" prop="buyerTaxId">
              <el-input v-model="form.buyerTaxId" placeholder="请输入购方识别号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="购买方名称" prop="buyerName">
              <el-input v-model="form.buyerName" placeholder="请输入购买方名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开票日期" prop="invoiceDate">
              <el-date-picker clearable
                              v-model="form.invoiceDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择开票日期"
                              style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票来源" prop="invoiceSource">
              <el-input v-model="form.invoiceSource" placeholder="请输入发票来源" />
            </el-form-item>
          </el-col>

          <!-- 票种与状态 -->
          <el-col :span="12">
            <el-form-item label="发票票种" prop="invoiceType">
              <el-select v-model="form.invoiceType" placeholder="请选择发票票种" clearable style="width: 100%;">
                <el-option
                    v-for="dict in dict.type.invoice_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票状态">
              <el-radio-group v-model="form.invoiceStatus">
                <el-radio
                    v-for="dict in dict.type.invoice_status"
                    :key="dict.value"
                    :label="dict.value"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否正数发票" prop="isPositiveInvoice">
              <el-select v-model="form.isPositiveInvoice" placeholder="请选择是否正数发票" clearable style="width: 100%;">
                <el-option
                    v-for="dict in dict.type.positive_invoice"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票风险等级" prop="invoiceRiskLevel">
              <el-input v-model="form.invoiceRiskLevel" placeholder="请输入发票风险等级" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开票人" prop="invoicePerson">
              <el-input v-model="form.invoicePerson" placeholder="请输入开票人" />
            </el-form-item>
          </el-col>

          <!-- 商品信息 -->
          <el-col :span="12">
            <el-form-item label="货物或应税劳务名称" prop="goodsName">
              <el-input v-model="form.goodsName" placeholder="请输入货物或应税劳务名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="specificationModel">
              <el-input v-model="form.specificationModel" placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量" prop="quantity">
              <el-input v-model="form.quantity" placeholder="请输入数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单价" prop="price">
              <el-input v-model="form.price" placeholder="请输入单价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="金额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税率" prop="taxRate">
              <el-input v-model="form.taxRate" placeholder="请输入税率" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税额" prop="taxAmount">
              <el-input v-model="form.taxAmount" placeholder="请输入税额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价税合计" prop="totalAmount">
              <el-input v-model="form.totalAmount" placeholder="请输入价税合计" />
            </el-form-item>
          </el-col>

          <!-- 其他字段 -->
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税收分类编码" prop="taxCategoryCode">
              <el-input v-model="form.taxCategoryCode" placeholder="请输入税收分类编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="删除标志" prop="delFlag">
              <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
            </el-form-item>
          </el-col>

        </el-row>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInvoice, getInvoice, delInvoice, addInvoice, updateInvoice } from "@/api/inv/outputnvoice";
// import { getDicts } from "@/api/system/dict/data";

export default {
  name: "Invoice",
  dicts: ['invoice_type','invoice_status','business_type','positive_invoice'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销项发票表格数据
      invoiceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        invoiceCode: null,        invoiceNumber: null,        eInvoiceNumber: null,        sellerTaxId: null,        sellerName: null,        buyerTaxId: null,        buyerName: null,        invoiceDate: null,        invoiceSource: null,        invoiceType: null,        invoiceStatus: null,        isPositiveInvoice: null,        invoiceRiskLevel: null,        invoicePerson: null,        taxCategoryCode: null,        specificBusinessType: null,        goodsName: null,        specificationModel: null,        unit: null,        quantity: null,        price: null,        amount: null,        taxRate: null,        taxAmount: null,        totalAmount: null,        status: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询销项发票列表 */
    getList() {
      this.loading = true;
      listInvoice(this.queryParams).then(response => {
        this.invoiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serialNum: null,        outId: null,        invoiceCode: null,        invoiceNumber: null,        eInvoiceNumber: null,        sellerTaxId: null,        sellerName: null,        buyerTaxId: null,        buyerName: null,        invoiceDate: null,        invoiceSource: null,        invoiceType: null,        invoiceStatus: "0",        isPositiveInvoice: null,        invoiceRiskLevel: null,        invoicePerson: null,        remark: null,        taxCategoryCode: null,        specificBusinessType: null,        goodsName: null,        specificationModel: null,        unit: null,        quantity: null,        price: null,        amount: null,        taxRate: null,        taxAmount: null,        totalAmount: null,        status: "0",        delFlag: null,        createBy: null,        createTime: null,        updateBy: null,        updateTime: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.serialNum)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销项发票";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const serialNum = row.serialNum || this.ids
      getInvoice(serialNum).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改销项发票";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.serialNum != null) {
            updateInvoice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvoice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const serialNums = row.serialNum || this.ids;
      this.$modal.confirm('是否确认删除销项发票编号为"' + serialNums + '"的数据项？').then(function() {
        return delInvoice(serialNums);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/invoice/export', {
        ...this.queryParams
      }, `invoice_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
